import DatePicker from 'vue-datepicker'
import Tab from '../tab/tab.vue'
import Pages from '../../../pages/pages.vue'
import { getSwitchList, saveSwitch, showMsg, setMenuType } from '../../../../vuex/action'
import getDatePickerOption from '../../../../util/date-picker-option'
export default {
  name: 'import',
  components: {
    'vue-tab': Tab,
    'vue-pages': Pages,
    'vue-date-picker': DatePicker
  },
  data() {
    return {
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      list: [],
      tradeNo: '',
      time: '',
      datePickerOption: getDatePickerOption(),
      tradePrice: '',
      tradeType: ''
    }
  },
  vuex: {
    getters: {
      fresh: state => state.dataflowListNeedFresh
    },
    actions: {
      xGetSwitchList: getSwitchList,
      xSaveSwitch: saveSwitch,
      xShowMsg: showMsg,
      xSetMenuType: setMenuType
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList()
  },
  methods: {
    getList() {
      this.xGetSwitchList()
        .then((data) => {
          this.list = data.data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    check(param) {
      for( let i in param) {
        var val = param[i] + ''
        if (!val.length) {
          return Promise.reject({
            error_msg: '* 标记的为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    save() {
      var param = {
        tradeNo: this.tradeNo,
        time: this.time,
        tradePrice: this.tradePrice,
        tradeType: this.tradeType
      }
      this.check(param)
      .then(() => {
        this.xSaveSwitch(param)
        .then(() => {
          this.getList()
        })
      })
      .catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
